package com.lottery.im.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.util.DisplayHelper;
import com.uuzuche.lib_zxing.DisplayUtil;

public class FastVideoView extends RelativeLayout {

  int mViewHeight = 0;

  private TextView mTvTime;
  private ImageView mIvOpen1;
  private ImageView mIvOpen2;
  private ImageView mIvOpen3;
  private TextView mTvTerm;
  private ImageView mIvResult1;
  private ImageView mIvResult2;
  private ImageView mIvResult3;
  private TextView mTvResult1;
  private TextView mTvResult2;
  private TextView mTvResult3;
  private AnimationDrawable mAnim1;
  private AnimationDrawable mAnim2;
  private AnimationDrawable mAnim3;

  public FastVideoView(Context context) {
    this(context, null);
  }

  public FastVideoView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public FastVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    // 开启硬件加速
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);

    View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_fast_video, this);
    view.findViewById(R.id.tv_close).setOnClickListener(v -> changeVisible());

    mTvTime = view.findViewById(R.id.tv_time_show);
    mIvOpen1 = view.findViewById(R.id.iv_open1);
    mIvOpen2 = view.findViewById(R.id.iv_open2);
    mIvOpen3 = view.findViewById(R.id.iv_open3);
    mTvTerm = view.findViewById(R.id.tv_term);
    mIvResult1 = view.findViewById(R.id.iv_result1);
    mIvResult2 = view.findViewById(R.id.iv_result2);
    mIvResult3 = view.findViewById(R.id.iv_result3);
    mTvResult1 = view.findViewById(R.id.tv_result1);
    mTvResult2 = view.findViewById(R.id.tv_result2);
    mTvResult3 = view.findViewById(R.id.tv_result3);

    mViewHeight = DisplayUtil.dip2px(getContext(), 20) + DisplayUtil.dip2px(getContext(), 30)
        + DisplayHelper.getScreenWidth(getContext()) * 597 / 1125;
  }

  /**
   * 倒计时阶段
   *
   * @param time 倒计时
   */
  public void setTime(String time) {
    cancel();
    mTvTime.setText(time);
  }

  /**
   * 显示最后总的结果
   *
   * @param num 期号
   * @param str 开奖结果数组
   * @param sum 总和
   * @param dx 大小
   * @param ds 单双
   */
  public void setTermAndResult(String num, String[] str, String sum, String dx, String ds) {
    mTvTerm.setText(num);
    mTvResult1.setText(sum);
    mTvResult2.setText(dx);
    mTvResult3.setText(ds);
    if (null != str && str.length >= 3) {
      mIvOpen1.setBackgroundResource(getResourceId("k3_" + str[0]));
      mIvOpen2.setBackgroundResource(getResourceId("k3_" + str[1]));
      mIvOpen3.setBackgroundResource(getResourceId("k3_" + str[2]));
      mIvResult1.setImageResource(getResourceId("sz_" + str[0]));
      mIvResult2.setImageResource(getResourceId("sz_" + str[1]));
      mIvResult3.setImageResource(getResourceId("sz_" + str[2]));
    }
  }

  /**
   * 正在开奖（正在获取开奖结果）
   */
  public void setOpening() {

    mTvTime.setText("待开奖");

    mIvOpen1.setBackground(getResources().getDrawable(R.drawable.k3_opening));
    mIvOpen2.setBackground(getResources().getDrawable(R.drawable.k3_opening));
    mIvOpen3.setBackground(getResources().getDrawable(R.drawable.k3_opening));

    mAnim1 = (AnimationDrawable) mIvOpen1.getBackground();
    mAnim2 = (AnimationDrawable) mIvOpen2.getBackground();
    mAnim3 = (AnimationDrawable) mIvOpen3.getBackground();

    mAnim1.start();
    mAnim2.start();
    mAnim3.start();
  }

  //根据图片名称取得对应图片
  private int getResourceId(String name) {
    int resId =
        getContext().getResources().getIdentifier(name, "drawable", getContext().getPackageName());
    //解析资源文件夹下，id为resId的图片
    return resId;
  }

  public void changeVisible() {
    if (getVisibility() == View.GONE) {
      animVisible();
    } else {
      animGone();
    }
  }

  private void animVisible() {
    ValueAnimator valueAnimator = createDropAnimator(1);
    valueAnimator.setDuration(300).start();
  }

  private void animGone() {
    ValueAnimator valueAnimator = createDropAnimator(2);
    valueAnimator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        setVisibility(View.GONE);
      }
    });
    valueAnimator.setDuration(300).start();
  }

  private ValueAnimator createDropAnimator(final int type) {
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(1, 1);
    valueAnimator.addUpdateListener(animation -> {
      ViewGroup.LayoutParams params = getLayoutParams();
      if (type == 1) {
        params.height = (int) (mViewHeight * animation.getAnimatedFraction());
        if (animation.getAnimatedFraction() > 0) setVisibility(View.VISIBLE);
      } else {
        params.height = (int) (mViewHeight * (1 - animation.getAnimatedFraction()));
      }
      setLayoutParams(params);
    });
    return valueAnimator;
  }

  public void cancel() {
    if (null != mAnim1) {
      if (mAnim1.isRunning()) mAnim1.stop();
      mAnim1 = null;
    }
    if (null != mAnim2) {
      if (mAnim2.isRunning()) mAnim2.stop();
      mAnim2 = null;
    }
    if (null != mAnim3) {
      if (mAnim3.isRunning()) mAnim3.stop();
      mAnim3 = null;
    }
  }
}
